;; 运行当前buffer
(add-to-list 'exec-path "/usr/local/bin")

;; (defun test ()
;;   ;; 最开始版本,不用了
;;   (interactive)
;;   (let* (
;;          (fName (replace-regexp-in-string ".*:" "" (buffer-file-name))) ;去除主机名,可以远程执行
;;          (fName-no-houzhui (replace-regexp-in-string "\\..*" "" fName)) ;去除后缀的文件名
;;          (fSuffix (file-name-extension fName)) ;后缀
;;          (cmdStr (concat  "/srv/lin/bin/rake test \""   fName "\"")) ;命令行
;;          )
;;     (when (buffer-modified-p)		;保存
;;       (save-buffer))
;;     (progn
;;       (message (concat "Running… " fSuffix "@" fName cmdStr))
;;       (shell-command cmdStr)
;;       (message (concat "运行完成" fSuffix "@" fName cmdStr))
;;       )))

(defun run-current-file ()
  (interactive)
  (let* (
         (suffixMap
          `(
            ("php" . "php")
            ("pt" . "phpunit")
            ("pl" . "perl")
            ;; ("py" . "/Library/Frameworks/Python.framework/Versions/3.9/bin/python3")
            ("py" . "python3")
            ("py3" . ,(if (string-equal system-type "windows-nt") "c:/Python32/python.exe" "python3"))
            ("rb" . "ruby")
	    ("rails" . "rails r ")
            ("js" . "/usr/local/opt/node@14/bin/node")             ; node.js
            ("sh" . "bash")
            ("exp" . "expect")
            ("ml" . "ocaml")
            ("vbs" . "cscript")
            ;; ("m" . "echo ")
            ("m" . "gcc `gnustep-config --objc-flags` -std=c99 -lgnustep-base -o /tmp/z")
	    ("swift" . "xcrun swift")
            ("dot" . "dot -Tpng ")             ;无用
            )
          )
         (fName (replace-regexp-in-string ".*:" "" (buffer-file-name))) ;去除主机名,可以远程执行
         (fName-no-houzhui (replace-regexp-in-string "\\..*" "" fName)) ;去除后缀的文件名
         (fSuffix (file-name-extension fName)) ;后缀
         (progName (cdr (assoc fSuffix suffixMap))) ;程序名
         (cmdStr (concat progName " \""   fName "\"")) ;命令行
         )
    ;; (when (buffer-modified-p)
    ;;   (when (y-or-n-p "Buffer modified. Do you want to save first?")
    ;;       (save-buffer) ) )
    (when (buffer-modified-p)
      (save-buffer))		    ;自动保存
    (if (string-equal fSuffix "el") ; emacs lisp 源代码
        (load (file-name-sans-extension fName))
      (if progName
          (progn
            (message (concat "Running… " fSuffix progName "@" fName))
            (cond                       ;根据后缀执行命令
             ((string-equal fSuffix "m") (shell-command (concat cmdStr ";/tmp/z"))) ;  objc
             ((string-equal fSuffix "dot") (progn
                                             ;; (message (concat cmdStr " -o " fName-no-houzhui ".png"))
                                             ;; (shell-command (concat cmdStr " -o " fName-no-houzhui ".png"))
                                             (graphviz-dot-preview)
                                             )) ;图片
	     ((and (string-equal fSuffix "py") (string-match "_spider" fName-no-houzhui)) (shell-command (concat "scrapy runspider -L ERROR " fName )))
	     ((and (string-equal fSuffix "py") (string-match "test_" fName-no-houzhui)) (shell-command (concat "pytest " fName )))
             (t (shell-command cmdStr))) ;运行其它程序
            (message (concat "运行完成" fSuffix "@" fName))
            )
        (message "后缀不认识")
        ))
    ))

(global-set-key (kbd "<f5>") 'run-current-file)

(provide 'run-config)
